package com.bookshop.servlet;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

public class HelloServlet extends HttpServlet {

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=utf-8");

        String name = req.getParameter("name");
        String age = req.getParameter("age");

        ServletConfig servletConfig = this.getServletConfig();
        String className = servletConfig.getInitParameter("className");

        String encoding = this.getServletContext().getInitParameter("encoding");

        System.out.println("上下文参数字符编码："+encoding);
        req.setCharacterEncoding(encoding);

        System.out.println(name);
        System.out.println(age);
        System.out.println(encoding);

        PrintWriter out = resp.getWriter();

        out.print("班级名称："+className+"<br/>");
        out.print("name:" + name + "<br>");
        out.print("age:" + age + "<br>");

        out.flush();
        out.close();

    }
}
